package com.lwj.liwanjia.utils;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.TimeAdapter;

import java.util.List;

/**
 * Created by Administrator on 2017/9/16.
 */

public class KuaiDiDialog extends Dialog implements View.OnClickListener {
    private Context context;
    private List<String> list;
    private TextView minute;//30分钟内
    private TextView feiyong;//费用
    private TextView hour;//2小时内
    private GridView gridTime;//今天
    private TextView sure;
    private int checkPostion = -1;
    private TimeAdapter adapter;

    public KuaiDiDialog(@NonNull Context context, List<String> list) {
        super(context, R.style.MyDialog);
        this.context = context;
        this.list = list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.kuaidi_shangmen_time);
        initView();
        initClick();
    }

    private void initClick() {
        minute.setOnClickListener(this);
        hour.setOnClickListener(this);
        gridTime.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                checkPostion = position;
                adapter.setCheckPostion(checkPostion);
                setImage(minute, false);
                setImage(hour, false);
                adapter.notifyDataSetChanged();
            }
        });
    }

    private void initView() {
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        setCanceledOnTouchOutside(false);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = params.MATCH_PARENT;
        params.height = params.WRAP_CONTENT;
        window.setAttributes(params);
        minute = (TextView) findViewById(R.id.tv_minute);
        feiyong = (TextView) findViewById(R.id.tv_feiyong);
        hour = (TextView) findViewById(R.id.tv_hour);
        sure = (TextView) findViewById(R.id.tv_sure);
        gridTime = (GridView) findViewById(R.id.grid_time);
        adapter = new TimeAdapter(context, list);
        adapter.setCheckPostion(checkPostion);
        gridTime.setAdapter(adapter);
        setColor(feiyong.getText().toString());
    }

    private void setColor(String str) {
        SpannableStringBuilder style = new SpannableStringBuilder(str);
        style.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.colorEC921A)), 4, str.lastIndexOf("元"), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        feiyong.setText(style);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_minute:
                checkPostion = -2;//半小时内上门
                setImage(minute, true);
                setImage(hour, false);
                adapter.setCheckPostion(checkPostion);
                adapter.notifyDataSetChanged();
                break;
            case R.id.tv_hour://两小时内上门
                checkPostion = -1;
                setImage(minute, false);
                setImage(hour, true);
                adapter.setCheckPostion(checkPostion);
                adapter.notifyDataSetChanged();
                break;
        }
    }

    /**
     * 获取选中的位置
     *
     * @return
     */
    public int getCheckPosition() {
        return checkPostion;
    }

    /**
     * 确定按钮
     *
     * @return
     */
    public TextView getSure() {
        return sure;
    }

    public void setImage(TextView tv, boolean b) {
        if (b) {
            Drawable drawable = context.getResources().getDrawable(R.mipmap.kuang_1);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(),
                    drawable.getMinimumHeight());
            tv.setCompoundDrawables(drawable, null, null, null);
            tv.setCompoundDrawablePadding(10);
        } else {
            Drawable drawable = context.getResources().getDrawable(R.mipmap.kuang);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(),
                    drawable.getMinimumHeight());
            tv.setCompoundDrawables(drawable, null, null, null);
            tv.setCompoundDrawablePadding(10);
        }
    }
}
